
var apiHost = wx.getStorageSync('apiHost');
var Appid = wx.getStorageSync('Appid');
var Secret = wx.getStorageSync('Secret');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    personObj:'',     //个人信息详情
    fansCount:0,      //粉丝总数
    followCount: 0,   //关注总数
    headImge: 0,      //头像
    nickName: 0,      //昵称
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var openId = wx.getStorageSync('openId');     //用户openId获取
    var userId = wx.getStorageSync('userId');     //用户userId获取
    console.log(userId)
    that.initPerson(userId);

    // 判断openid弹窗授权
    if (openId != '') {
      that.setData({
        openId: openId,
        userId: userId,
        showCancelArea: false
      });
    } else {
      that.setData({
        showCancelArea: true
      });
    }
  },

  /**
   * 初始化个人信息
   */
  initPerson: function (userId){
    var that = this;
    wx.showLoading({
      title: '加载中',
      icon: 'loading',
      mask: true
    })
    wx.request({
      url: apiHost + 'hpp/user/personalCenter',
      method: 'get',
      header: {
        'content-type': 'application/json'
      },
      data: {
        userId: userId
      },
      success: function (res) {
        console.log(res)
        var code = res.data.code;
        var msg = res.data.msg;
        if ("10000" == code) {
          var personObj = res.data.data;

          that.setData({
            personObj: personObj
          })
          
        } else {
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 2000,
            mask: false
          })
        }
        wx.hideLoading();
      },
      fail: function () {
        wx.hideLoading();
        wx.showToast({
          title: '网络异常',
          icon: 'none',
          duration: 2000,
          mask: false
        })
      }
    })
  },
  /**
   * 团员订单管理
   */
  goLeagueOrderList: function () {
    this.goPage('../../orders/leagueOrderList/leagueOrderList');
  },
  //团长订单管理
  goHearOrderList: function () {
    this.goPage('../../orders/hearOrderList/hearOrderList');
  },
  //跳转到设置
  Setting: function () {
    this.goPage('../Setting/Setting');
  },
  //跳转到关于惠拼团
  aboutHuipt: function () {
    this.goPage('../aboutHuipt/aboutHuipt');
  },
  //编辑个人资料
  goEditInfo: function () {
    var that = this;
    if(that.data.userId == ''){
      wx.showToast({
        title: 'userId为空',
        icon:'none',
      })
      return;
    }
    this.goPage('../../personal/personEditInfo/personEditInfo');
  },
  // 地址管理
  bindAddress: function () {
    this.goPage('../../personal/personAddress/personAddressList/personAddressList');
  },
  //关注跳转
  attention: function () {
    this.goPage('../../personal/attention/attention');
  },
  //粉丝跳转
  fans: function () {
    this.goPage('../../personal/fans/fans');
  },
  //我发的团跳转
  Mission: function () {
    this.goPage('../../personal/Mission/Mission');
  },
  //我参的团跳转
  addgroup: function () {
    this.goPage('../../personal/addgroup/addgroup');
  },
  //自提点设置
  ZTdot: function () {
    this.goPage('../../personal/setOneselfRaiseDot/setOneselfRaiseDot');
  },
  //收入明细
  income: function () {
    this.goPage('../../personal/income/income');
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },
  /**
   * 跳转页面
   */
  goPage : function(url){
    wx.showLoading({
      title: '跳转中',
      icon: 'loading',
    })
    wx.navigateTo({
      url: url,
      success: function(){
        wx.hideLoading();
      }
    })
  },
  //按钮获取用户信息
  bindGetUserInfo: function (res) {
    var that = this;

    var errMsgFlg = res.detail.errMsg

    if ("getUserInfo:ok" == errMsgFlg) {
      var userCon = res.detail
      wx.login({
        success: function (r) {
          var code = r.code;//登录凭证
          if (code) {
            that.VERBurlUser(userCon, code)
          } else {
            wx.showToast({
              title: '获取用户登录态失败！' + r.errMsg,
              icon: 'none',
              duration: 2000,
              mask: true
            });
          }
        },
        fail: function () {
          wx.hideLoading();   //隐藏加载loading
          wx.showToast({
            title: '登陆失败',
            icon: 'none',
            duration: 2000,
            mask: true
          });
        }
      })
    }
  },

  /**
  * 解密用户登录完成后的url
  */
  VERBurlUser: function (res, code) {
    var that = this;
    wx.showLoading({
      title: '加载中',
      icon: 'loading',
    });
    //3.请求自己的服务器，解密用户信息 获取unionId等加密信息  (注意：这个东西必须需要 开放平台 授权)
    wx.request({
      url: apiHost + 'hpp/UserInfo/decodeUserInfo',//自己的服务接口地址
      method: 'post',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        encryptedData: res.encryptedData,
        iv: res.iv,
        code: code,
        Appid: Appid,
        Secret: Secret
      },
      success: function (data) {
        //4.解密成功后 获取自己服务器返回的结果

        if (data.data.status == 1) {
          var userInfo_ = data.data.userInfo;

          var openIdFl = userInfo_.openId;
          var phone = userInfo_.phone;

          that.getuserId(userInfo_.openId);       //获取userId
          wx.setStorageSync('openId', openIdFl);
          wx.setStorageSync('phone', phone);
          wx.setStorageSync('session_token', userInfo_.session_key);
          wx.setStorageSync('userInfo', userInfo_);

          that.setData({
            showCancelArea: false,
            userInfo: userInfo_
          });
          that.onLoad();
        } else {
          wx.hideLoading();   //隐藏加载loading
          wx.showToast({
            title: '网络异常',
            icon: 'none',
            duration: 2000,
            mask: true
          });
        }
        wx.hideLoading();   //隐藏加载loading
      },
      fail: function () {
        wx.hideLoading();   //隐藏加载loading
        wx.showToast({
          title: '网络异常',
          icon: 'none',
          duration: 2000,
          mask: true
        });
        wx.hideLoading();   //隐藏加载loading
      }
    })
  },
  /**
   * 获取userId
   */
  getuserId: function (openId) {
    var that = this;
    wx.showLoading({
      title: '加载中',
      icon: "loading",
      mask: true
    });

    wx.request({
      url: apiHost + 'hpp/user/userInfoByOpenId',
      method: 'GET',
      header: {
        "content-type": 'application/json'
      },
      data: {
        openId: openId
      },
      success: function (res) {
        var code = res.data.code;
        var msg = res.data.msg;
        var userIdFlag = that.data.userId;

        wx.hideLoading();   //隐藏加载loading
        if ("10000" == code) {
          var userId = res.data.data.id;

          that.setData({
            userId: userId
          });

          wx.setStorageSync("userId", userId)
        } else {
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 5000,
            mask: true
          });
        }
      },
      fail: function () {
        wx.hideLoading();   //隐藏加载loading
        wx.showToast({
          title: '网络异常',
          icon: 'none',
          duration: 5000,
          mask: true
        });
      }
    })
  },
})